package cn.com.agree.bigdata.vmdatahandle.service;

import ch.qos.logback.core.util.ExecutorServiceUtil;
import cn.com.agree.bigdata.vmdatahandle.config.AppConfig;
import cn.com.agree.bigdata.vmdatahandle.config.AppConfigUtil;
import cn.com.agree.bigdata.vmdatahandle.exception.CustomAppException;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: LiYuan
 * @date: 2024/7/9 15:29
 */

@Slf4j
public class ReadExporterWriteVm {
    private AppConfig appConfig;

    static {
        Unirest.config()
            .socketTimeout(120000)
            .connectTimeout(120000)
            .concurrency(300, 300);
    }


    public void run() throws CustomAppException {
        long startTime = System.currentTimeMillis();
        log.info("开始时间:{}", startTime);
        appConfig = AppConfigUtil.getAppConfig();
        int interval = appConfig.getNodeExporter().getInterval();
        if (interval <= 0) {
            interval = 15;
        }

        ExecutorService executor = null;
        int threadsNum = appConfig.getVm().getThreads();
        if (threadsNum > 0) {
            executor = Executors.newFixedThreadPool(threadsNum);
        }
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.scheduleWithFixedDelay(new WriteWmWorker(appConfig, executor), 2, interval, TimeUnit.SECONDS);

//        new WriteWmWorker(appConfig, executor).run();
        long endTime = System.currentTimeMillis();
        log.info("结束时间:{}, 总共耗时:{}秒", endTime, (endTime - startTime) / 1000.0);
    }

}
